
const gulp = require('gulp');
const less = require('gulp-less');
const autoprefixer = require('autoprefixer');
const cssnano = require('cssnano');
const postcss = require('gulp-postcss');
const plumber = require('gulp-plumber');
const sourcemaps = require('gulp-sourcemaps');
const uglify = require('gulp-uglify');
const gconcat = require('gulp-concat');
const htmlmin = require('gulp-htmlmin');
const babel = require("gulp-babel");
const rollup = require('gulp-rollup');

const b_src = 'src';
const b_dist = 'public';


// 合并 js
gulp.task('js', function() {
  return gulp.src(b_src + '/js/*.js')
      .pipe(plumber({
        errorHandler(err) {
          console.log(err);
          this.emit('end');
        }
      }))
      .pipe(sourcemaps.init())
      .pipe(rollup({
        format: 'iife',
        input: b_src + '/js/main.js'
      }))
      .pipe(babel())
      .pipe(uglify())
      .pipe(sourcemaps.write())
      .pipe(gulp.dest(b_dist + '/js'));
});


// 编译样式
gulp.task('css', function() {
  return gulp.src(b_src + '/style/master.less')
      .pipe(plumber({
        errorHandler(err) {
          console.log(err);
          this.emit('end');
        }
      }))
      .pipe(sourcemaps.init())
      .pipe(less())
      .pipe(postcss([autoprefixer(), cssnano({
        zindex: false,
        normalizeUrl: false,
        normalizeUnicode: false,
        minifyFontValues: false
      })]))
      .pipe(sourcemaps.write())
      .pipe(gulp.dest(b_dist + '/css'));
});


// 移动文件
gulp.task('static', function() {
  return gulp.src(b_src + '/static/**/*')
      .pipe(plumber({
          errorHandler(err) {
              console.log(err);
              this.emit('end')
          }
      }))
      .pipe(gulp.dest(b_dist));
});


// DEV
gulp.task('dev', ['js', 'css', 'static'], function() {

  console.log('[工作中..] 👷 👷');
  gulp.watch(b_src + '/js/*.js', ['js']);
  gulp.watch(b_src + '/style/**/*.less', ['css']);
  gulp.watch(b_src + '/static/**/*', ['static']);
});
